//
//  CTPersonalProfileDetailsViewController.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/6/30.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTPersonalProfileDetailsViewController.h"


//view
#import "CTPersonalNameModifyView.h"
#import "CTPersonalGenderModifyView.h"
#import "CTPersonalSignModifyView.h"


//logic
#import "CTThirdPartyRegisterLogic.h"

//function
#import "CTDebugManager.h"
#import "CTAppLanguage.h"
#import "CTRemindView.h"
#import "CTTextManager.h"
#import "CTActivitorView.h"
#import "CTLogin.h"
#import "MobClick.h"

//model
#import "CTUserItem.h"

@interface CTPersonalProfileDetailsViewController ()<CTPersonalNameModifyViewDelegate,CTPersonalGenderModifyViewDelegate,CTPersonalSignModifyViewDelegate>
{
    CTPersonalNameModifyView * nameView;
    CTPersonalGenderModifyView * genderView;
    CTPersonalSignModifyView * signView;
    
    CTThirdPartyRegisterLogic * logic;
    
    CTUserInfoModifyRequestItem * modifyItem;
    CTActivitorView * acView;
}
@end

@implementation CTPersonalProfileDetailsViewController

@synthesize type,nameStr,genderStr,signStr;

- (void)dealloc
{
    logic =nil;
    nameStr = nil;
    genderStr = nil;
    modifyItem = nil;
    
    [acView removeFromSuperview];
    acView = nil;
    
    [nameView removeFromSuperview];
    nameView = nil;
    
    [genderView removeFromSuperview];
    genderView = nil;
    
    [signView removeFromSuperview];
    signView = nil;
    
     [CTDebugManager showDebugLogInfo:@"dealloc : CTPersonalProfileDetailsViewController Is Removed!!!"];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        logic = [[CTThirdPartyRegisterLogic alloc] init];
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    //统计
    [MobClick beginLogPageView:@"CTPersonalProfileDetailsViewController"];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    //统计
    [MobClick endLogPageView:@"CTPersonalProfileDetailsViewController"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    switch (type) {
        case CTPersonalProfileDetailsTypeName:
            nameView = [[CTPersonalNameModifyView alloc] init];
            nameView.delegate = self;
            nameView.nameLabelStr = nameStr;
            [self.view addSubview:nameView];
            break;
        case CTPersonalProfileDetailsTypeGender:
            genderView = [[CTPersonalGenderModifyView alloc] init];
            genderView.delegate  =self;
            genderView.sexStr = genderStr;
            [self.view addSubview:genderView];
            break;
        case CTPersonalProfileDetailsTypeSign:
            signView = [[CTPersonalSignModifyView alloc] init];
            signView.delegate = self;
            signView.signStr = signStr;
            [self.view addSubview:signView];
            break;
            
        default:
            break;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}





#pragma mark - name view delegate -


- (void)personalNameModifyViewSelectedBackBtnAction:(CTPersonalNameModifyView *)view
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)personalNameModifyViewSelectedSaveBtnAction:(CTPersonalNameModifyView *)view withSexString:(NSString *)sexStr
{
    if ([[CTTextManager shareInstance] isBlankString:sexStr] == YES) {
        CTRemindView * remindView = [[CTRemindView alloc] init];
        [remindView showRemindViewOn:self.view withRemindWord:@"不能为空哦!"];
        return;
    }
    
    
    NSString * languageStr = [CTAppLanguage systemLanguage];
   
    modifyItem = nil;
    modifyItem = [[CTUserInfoModifyRequestItem alloc] init];
    modifyItem.token = [CTLogin token];
    modifyItem.name = sexStr;
    if ([languageStr isEqualToString:@"zh-Hans"])modifyItem.lang = @"cn";
    else modifyItem.lang = @"en";
    
    acView = [[CTActivitorView alloc] init];
    [acView showActivitorViewOn:self.view];
    
    [logic thirdPartyRegisterRequest:modifyItem successCompletionHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [acView removeFromSuperview];
            acView = nil;
            
            [self performSelector:@selector(backAction:) withObject:nil afterDelay:0.3];
        });
        
    } failedHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [acView removeFromSuperview];
            acView = nil;
            
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:@"修改失败!"];
        });
    }];
}



#pragma mark - gender view delegate -


- (void)personalGenderModifyViewSelectedBackBtnAction:(CTPersonalGenderModifyView *)view
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)personalGenderModifyViewSelectedSaveBtnAction:(CTPersonalGenderModifyView *)view withSexString:(NSString *)sexStr
{
   
    if ([[CTTextManager shareInstance] isBlankString:sexStr] == YES) {
        CTRemindView * remindView = [[CTRemindView alloc] init];
        [remindView showRemindViewOn:self.view withRemindWord:@"不能为空哦!"];
        return;
    }
   
    if ([sexStr isEqualToString:@"0"]) {
        CTRemindView * remindView = [[CTRemindView alloc] init];
        [remindView showRemindViewOn:self.view withRemindWord:@"请选择性别"];
        return;
    }
    
    NSString * languageStr = [CTAppLanguage systemLanguage];
    modifyItem = nil;
    modifyItem = [[CTUserInfoModifyRequestItem alloc] init];
    modifyItem.token = [CTLogin token];
    modifyItem.gender = sexStr;
    if ([languageStr isEqualToString:@"zh-Hans"])modifyItem.lang = @"cn";
    else modifyItem.lang = @"en";
    
    acView = [[CTActivitorView alloc] init];
    [acView showActivitorViewOn:self.view];
    
    [logic thirdPartyRegisterRequest:modifyItem successCompletionHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [acView removeFromSuperview];
            acView = nil;
            
            [self performSelector:@selector(backAction:) withObject:nil afterDelay:0.3];
        });
        
    } failedHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [acView removeFromSuperview];
            acView = nil;
            
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:@"修改失败!"];
        });
    }];
}




#pragma mark - sign view delegate -

- (void)BackBtnAction:(CTPersonalSignModifyView *)view
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)personalSignModifyViewSelectedSaveBtnAction:(CTPersonalSignModifyView *)view withSexString:(NSString *)sexStr
{
    
    if ([[CTTextManager shareInstance] isBlankString:sexStr] == YES) {
        CTRemindView * remindView = [[CTRemindView alloc] init];
        [remindView showRemindViewOn:self.view withRemindWord:@"不能为空哦!"];
        return;
    }
    
    
    NSString * languageStr = [CTAppLanguage systemLanguage];
    modifyItem = nil;
    modifyItem = [[CTUserInfoModifyRequestItem alloc] init];
    modifyItem.token = [CTLogin token];
    modifyItem.sign = sexStr;
    if ([languageStr isEqualToString:@"zh-Hans"])modifyItem.lang = @"cn";
    else modifyItem.lang = @"en";
    
    acView = [[CTActivitorView alloc] init];
    [acView showActivitorViewOn:self.view];
    
    [logic thirdPartyRegisterRequest:modifyItem successCompletionHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [acView removeFromSuperview];
            acView = nil;
            
            [self performSelector:@selector(backAction:) withObject:nil afterDelay:0.3];
        });
        
    } failedHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [acView removeFromSuperview];
            acView = nil;
            
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:@"修改失败!"];
        });
    }];
}


#pragma mark - action -

-(void)backAction:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
